package com.platform.modules.cpt.controller;

import com.platform.common.utils.RestResponse;
import com.platform.modules.cpt.entity.CptAcceptanceConditionEntity;
import com.platform.modules.cpt.entity.CptAcceptanceEntity;
import com.platform.modules.cpt.service.CptAcceptanceConditionService;
import com.platform.modules.quality.vo.CommonResult;
import com.platform.modules.sys.controller.AbstractController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * Controller
 *
 * @author biaohuaming
 * @date 2020-02-24 15:28:20
 */
@Api(tags = "竣工验收监督条件核查")
@RestController
@RequestMapping("cpt/acceptance/condition")
public class CptAcceptanceConditionController extends AbstractController {


    @Autowired
    private CptAcceptanceConditionService cptAcceptanceConditionService;



    /**
     * @descr 查询<br>
     * @param id
     * @return
     */
    @ApiOperation(value = "查询")
    @GetMapping("/view")
    public RestResponse view(@RequestParam String cptAcceptanceId) {
    	Map<String, Object> params = getParams();
    	params.put("cptId", cptAcceptanceId);
    	CptAcceptanceConditionEntity entity = cptAcceptanceConditionService.queryOne(params);
        return RestResponse.success().put("data", entity);
    } 
    
    /**
     * @param cptAcceptanceTeamEntity
     * @return
     */
    @ApiOperation(value = "保存")
    @PostMapping("/save")
    public CommonResult save(@RequestBody CptAcceptanceConditionEntity cptAcceptanceConditionEntity) {
    	if(StringUtils.isBlank(cptAcceptanceConditionEntity.getId())) {
    		//未签名
    		cptAcceptanceConditionEntity.setIsSign("1");
    	}
    	cptAcceptanceConditionService.saveOrUpdate(cptAcceptanceConditionEntity);
        return CommonResult.success();
    }
    
    /**
     * 生成 工程竣工验收监督条件核查记录一(后续盖章)
     * @param cptAcceptanceEntity
     * @return
     */
    @ApiOperation(value = "生成-工程竣工验收监督条件核查记录一(后续盖章)")
    @PostMapping("/gcjgysjdtjhcjl1File/create")
    public CommonResult creategcjgysjdtjhcjl1File(@RequestBody CptAcceptanceEntity cptAcceptanceEntity) {
    	Map<String, Object> result = cptAcceptanceConditionService.createGcjgysjdtjhcjl1File(cptAcceptanceEntity);
        return CommonResult.success(result);
    }
    
    @ApiOperation(value = "下载-工程竣工验收监督条件核查记录一 文件流")
    @GetMapping("/gcjgysjdtjhcjl1File/download")
    public void downloadgcjgysjdtjhcjl1File(@RequestParam String cptId, HttpServletRequest request, HttpServletResponse response) {
    	cptAcceptanceConditionService.downloadGcjgysjdtjhcjlFile(cptId, request, response, "CPT_ACCPTANCE_FILES_GCJGYSJDTJHCJL1");
    }
    
    @ApiOperation(value = "下载-工程竣工验收监督条件核查记录文件")
    @GetMapping("/gcjgysjdtjhcjl1File/sign/download")
    public void downloadSignGcjgysjdtjhcjl1File(@RequestParam String cptId, HttpServletRequest request, HttpServletResponse response) {
    	cptAcceptanceConditionService.downloadSignGcjgysjdtjhcjlFile(cptId, request, response, "CPT_ACCPTANCE_FILES_GCJGYSJDTJHCJL1_SIGN");
    }
    

    @ApiOperation(value = "盖章-工程竣工验收监督条件核查记录一 文件")
    @PostMapping("/gcjgysjdtjhcjl1File/sign")
    public RestResponse signgcjgysjdtjhcjl1File(HttpServletRequest request,MultipartFile file, 
    		@RequestParam String cptId, 
    		@RequestParam String attId,
    		@RequestParam(required=false) String fileName) {
    	
    	//获取已签章的合同
        String contentType = request.getContentType();
        
        if (contentType != null && contentType.toLowerCase().startsWith("multipart/")) {
        	cptAcceptanceConditionService.signGcjgysjdtjhcjlFile(request, cptId, "CPT_ACCPTANCE_FILES_GCJGYSJDTJHCJL1_SIGN", "工程竣工验收监督条件核查记录一.pdf");
        }else {
        	RestResponse.error();
        }
        return RestResponse.success();
    }
    

    /**
     * 生成 工程竣工验收监督条件核查记录二(后续盖章)
     * @param cptAcceptanceEntity
     * @return
     */
    @ApiOperation(value = "生成-工程竣工验收监督条件核查记录二(后续盖章)")
    @PostMapping("/gcjgysjdtjhcjl2File/create")
    public CommonResult creategcjgysjdtjhcjl2File(@RequestBody CptAcceptanceEntity cptAcceptanceEntity) {
    	Map<String, Object> result = cptAcceptanceConditionService.createGcjgysjdtjhcjl2File(cptAcceptanceEntity);
        return CommonResult.success(result);
    }
    
    @ApiOperation(value = "下载-工程竣工验收监督条件核查记录二 文件流")
    @GetMapping("/gcjgysjdtjhcjl2File/download")
    public void downloadgcjgysjdtjhcjl2File(@RequestParam String cptId, HttpServletRequest request, HttpServletResponse response) {
    	cptAcceptanceConditionService.downloadGcjgysjdtjhcjlFile(cptId, request, response, "CPT_ACCPTANCE_FILES_GCJGYSJDTJHCJL2");
    }
    
    @ApiOperation(value = "下载-工程竣工验收监督条件核查记录文件")
    @GetMapping("/gcjgysjdtjhcjl2File/sign/download")
    public void downloadSignGcjgysjdtjhcjl2File(@RequestParam String cptId, HttpServletRequest request, HttpServletResponse response) {
    	cptAcceptanceConditionService.downloadSignGcjgysjdtjhcjlFile(cptId, request, response, "CPT_ACCPTANCE_FILES_GCJGYSJDTJHCJL2_SIGN");
    }
    

    @ApiOperation(value = "盖章-工程竣工验收监督条件核查记录二 文件")
    @PostMapping("/gcjgysjdtjhcjl2File/sign")
    public RestResponse signgcjgysjdtjhcjl2File(HttpServletRequest request,MultipartFile file, 
    		@RequestParam String cptId, 
    		@RequestParam String attId,
    		@RequestParam(required=false) String fileName) {
    	
    	//获取已签章的合同
        String contentType = request.getContentType();
        
        if (contentType != null && contentType.toLowerCase().startsWith("multipart/")) {
        	cptAcceptanceConditionService.signGcjgysjdtjhcjlFile(request, cptId, "CPT_ACCPTANCE_FILES_GCJGYSJDTJHCJL2_SIGN", "工程竣工验收监督条件核查记录二.pdf");
        }else {
        	RestResponse.error();
        }
        return RestResponse.success();
    }
}
